package com.lzq.train.business.mq;

import com.alibaba.fastjson.JSON;
import com.lzq.train.business.dto.ConfirmOrderMQDto;
import com.lzq.train.business.req.ConfirmOrderDoReq;
import com.lzq.train.business.service.ConfirmOrderService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RocketMQMessageListener(topic = "CONFIRM_ORDER", consumerGroup = "default")
public class ConfirmOrderConsumer implements RocketMQListener<MessageExt> {

    private final static Logger LOG = LoggerFactory.getLogger(ConfirmOrderConsumer.class);

    @Autowired
    private ConfirmOrderService confirmOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        ConfirmOrderMQDto dto = JSON.parseObject(new String(body), ConfirmOrderMQDto.class);
        MDC.put("LOG_ID", dto.getLogId());
        LOG.info("RocketMq 收到消息：{}", new String(body));

        // 消费消息
        confirmOrderService.doConfirm(dto);
    }
}
